-- UIPetRecruit
-- Create by cheny3
-- 冈布奥招募界面

require "game/ui/form/shop/UITankDesc"

-- UIPetRecruit继承自Layer
UIPetRecruit = class("UIPetRecruit", function()
    return cc.Layer:create();
end);

function UIPetRecruit.create(campId, petList)
    return UIPetRecruit.new(campId, petList);
end

-- 内部函数声明
local campType = {4, 3, 1, 2};
-- 模型动作播放序列
local heroActions = {HERO_ACTION_DAMAGED, HERO_ACTION_ATTACK, HERO_ACTION_CAST_ME};

-- 构造函数
function UIPetRecruit:ctor(campId, petList)
    -- 初始化
    self:setName("UIPetRecruit");
    local node = cc.CSLoader:createNode("layout/shop/PetRecruit.csb");
    self:addChild(node);
    self.node = node;
    self.campId = campId;
    self.petList = petList;

    local CT = node:getChildByName("CT");
    local scrollview = CT:getChildByName("scrollview");
    self.scrollview = scrollview;
    self.scrollview:setVisible(true);

    -- 修改滚动方向
    -- 1 垂直
    -- 2 水平
    self.scrollview:setDirection(2);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();

    if self.petList and #petList == 1 then
        self.scrollview:setVisible(false);
    end

    -- TOP修改模式
    -- 为了UIPetRecruitMain执行淡入淡出时该节点不起效
    local topNode = findChildByName(self.node, "TOP/title_node/bg");
    setGrayTransMode(topNode, false);
end

-- 适配
function UIPetRecruit:resize()
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.alignToCenter(node, "bg");
    local heroBg = findChildByName(node, "bg/hero_bg");
    if h / w < 1.5 then
        heroBg:setScaleX(768 / DESIGN_WIDTH);
    end

    local offSet = math.max(h / w - 1.5, 0) * 150;
    local tip = findChildByName(self.node, "CT/tip");
    tip:setPositionY(tip:getPositionY() - offSet);
    self.scrollview:setPositionY(self.scrollview:getPositionY() - offSet);

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- TOP顶部居中并缩放至合适大小
    AlignM.alignToTopCenter(node, "TOP");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIPetRecruit:redraw()
    self:redrawCampInfo();

    -- 收集商品信息
    local goodsList = self:gatherGoodsInfo();
    if #goodsList == 0 then
        self:redrawNoPet();
        return;
    end
    self:initGoodsGrids();
    self:redrawGoods();
end

-- 没有宠物信息
function UIPetRecruit:redrawNoPet()
    local tip = findChildByName(self.node, "CT/tip");
    local petNode = findChildByName(self.node, "CT/pet_node");
    local costNode = findChildByName(self.node, "CT/cost_node");
    local btnDesc = findChildByName(self.node, "BT/btn_desc");
    local clickPanel = findChildByName(self.node, "CT/click_panel");
    local btnTip = findChildByName(self.node, "BT/btn_desc/hint");
    costNode:setVisible(false);
    clickPanel:setVisible(false);
    self.scrollview.rows = {};
    self.scrollview:removeAllChildren();
    TextStyleM.setTextStyle(tip, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, -4);
    tip:setString(getLocStr("pet_sold_out_tips"));
    btnDesc:setTitleText(getLocStr("btn_text_help"));
    btnTip:setVisible(false);

    local image = ccui.ImageView:create();
    image:loadTexture(getHeroSilhouetteIconPath("slime_shadow"));
    image:setPositionY(-50);
    self.iconImg = nil;
    petNode:removeAllChildren();
    petNode:addChild(image);
end

-- 获取可购买的商品信息
function UIPetRecruit:gatherGoodsInfo()
    return self:getRecruitInfo();
end

-- 佣兵营地
function UIPetRecruit:getRecruitInfo()
    -- 获取本店的所有商品
    -- 按阵营获取
    local goodsList;
    if type(self.petList) == "table" then
        goodsList = PetShopM.getAllGoods();
    else
        goodsList = PetShopM.getGoodsByCamp(self.campId);
    end

    if goodsList == nil then
        return nil;
    end

    local ret = {};
    local goodsIndex = 1;  -- 商品的序号
    for _, info in ipairs(goodsList) do
        local goodsId    = info.id;
        local arr;
        arr = PetShopM.queryPrice(goodsId);

        local payAttrib  = arr[2];           -- 支付属性
        local goodsPrice = arr[3];           -- 商品价格
        local classId    = info["pet_id"];
        local petInfo    = PetM.query(classId);
        local activity   = info["activity"]; -- 活动副本专属
        if not ME.user.pets[classId] then
            if not self.petList and (activity ~= 1 or PetShopM.canActivityPetHire(classId)) then
                -- 佣兵兑换的
                table.insert(ret, {goodsIndex, goodsId, classId, PetM.getDefaultIcon(classId), payAttrib, goodsPrice,
                    PetM.query(classId, "name"), petInfo["init_rank"]});
            elseif self.petList and table.indexOf(self.petList, classId) > 0 then
                -- 活动本专属的
                table.insert(ret, {goodsIndex, goodsId, classId, PetM.getDefaultIcon(classId), payAttrib, goodsPrice,
                    PetM.query(classId, "name"), petInfo["init_rank"]});
            end
        end

        goodsIndex = goodsIndex + 1;
    end

    return ret;
end

-- 显示商品信息
function UIPetRecruit:redrawGoods()
    -- 显示商品信息
    local goodsList = self:gatherGoodsInfo();
    local totalGoodsCount = #goodsList;
    self.iconImgList = {};
    for i = 1, totalGoodsCount do
        local grid          = self.scrollview:getChildByName("item" .. i);
        local goodsBg       = grid:getChildByName("bg");
        local iconImg       = grid:getChildByName("goods_icon");
        local rankNode      = grid:getChildByName("star_node");
        local focus         = grid:getChildByName("focus");
        local hint          = grid:getChildByName("hint");

        if i <= totalGoodsCount then
            grid:setVisible(true);
            local goodsIndex = goodsList[i][1];   -- 商品序号
            local goodsId    = goodsList[i][2];   -- 商品id
            local classId    = goodsList[i][3];   -- 宠物class_id
            local goodsIcon  = goodsList[i][4];   -- 商品图标
            local payAttrib  = goodsList[i][5];   -- 支付属性
            local goodsPrice = goodsList[i][6];   -- 商品价格
            local itemName   = goodsList[i][7];   -- 物品名称
            local rank       = goodsList[i][8];   -- 阶位

            -- 为商品图标绑定信息
            iconImg.goodsIndex = goodsIndex;
            iconImg.goodsId    = goodsId;
            iconImg.classId    = classId;
            iconImg.goodsIcon  = goodsIcon;
            iconImg.payAttrib  = payAttrib;
            iconImg.goodsPrice = goodsPrice;
            iconImg.itemName   = itemName;
            iconImg.rank       = rank;
            iconImg.goodsType  = self.shopType;
            iconImg.focus      = focus;
            iconImg.hint       = hint;
            iconImg.index      = i;

            -- 如果碎片足够则提示
            if ItemM.getAmount(ME.user, payAttrib) >= goodsPrice then
                hint:setVisible(true);
                playBubbleAnima(hint, false);
            else
                hint:setVisible(false);
            end

            -- 显示商品阶位
            local layer = createStarLayer(rank, "images/ui/symbol/star_little.png", "center", -0.3);
            if layer then
                rankNode:removeAllChildren();
                rankNode:addChild(layer);
            end

            -- 显示商品图标
            iconImg:loadTexture(getHeroIconPath(goodsIcon));

            -- 显示星星
            rankNode:setVisible(true);

            local defaultPet = PetShopM.getCanHireSlimeByCamp(self.campId);
            if defaultPet > 0 and classId == defaultPet then
                -- 选择提示的冈布奥
                self:onGoodsClick(iconImg);
            elseif defaultPet == 0 and i == 1 then
                -- 默认选中第一个
                self:onGoodsClick(iconImg);
            elseif self.petList and i == 1  then
                -- 指定了冈布奥
                self:onGoodsClick(iconImg);
            end

            table.insert(self.iconImgList, iconImg);
        else
            -- 清空多余的格子
            grid:setVisible(false);
        end
    end
end

-- 初始化商品格
function UIPetRecruit:initGoodsGrids()
    local w = DESIGN_WIDTH;
    local h = DESIGN_HEIGHT;

    -- 图标间距(横向)
    local wGap = 0;

    -- 图标间距(纵向)
    local hGap = 0;

    -- 图标宽度
    local iconWidth = 157;

    -- 图片高度
    local iconHeight = 171;

    local topHGap  = 15;
    local leftWGap = 0;

    -- 收集商品信息
    local goodsList = self:gatherGoodsInfo();

    -- 修正位置
    if #goodsList == 3 then
        leftWGap = iconWidth / 2;
    elseif #goodsList == 2 then
        leftWGap = iconWidth;
    elseif #goodsList == 1 then
        leftWGap = iconWidth * 3 / 2;
    end

    -- 商品的共个数
    local shopCount = #goodsList;

    self.scrollview.rows = {};
    self.scrollview:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/shop/PetShopItem2.csb");

    -- @param windowNode    界面节点
    -- @param scrollNode    容器节点
    -- @param itemNode      单元格的节点
    -- @param totalNum      单元格总数
    -- @param maxRowNum     最大行数
    -- @param pageColNum    单页可以显示下的列数
    -- @param wGap          单元格间距(横向) 这里作为最小值使用
    -- @param hGap          单元格间距(纵向)
    -- @param iconWidth     单元格宽度
    -- @param iconHeight    单元格高度
    -- @param topHGap       上方留
    -- @param leftWGap      左右留白  这里作为最小值使用
    -- @param touchEvent    点击事件注册函数
    initScrollGridsHorizontal(self, self.scrollview, itemNode, shopCount, 1, shopCount, wGap, hGap, iconWidth, iconHeight, topHGap, leftWGap, self.registerPetShopTouchEvent);
end

-- 注册商品点击事件
function UIPetRecruit:registerPetShopTouchEvent(node)
    -- 注册商品图标点击事件
    local image = node:getChildByName("bg");
    local iconImg = node:getChildByName("goods_icon");
    local function onClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_spell");
            if type(iconImg.goodsIcon) == 'number' then
                -- 如果当前选中自身，不处理
                if self.iconImg ~= iconImg then
                    self:onGoodsClick(iconImg);
                end
            end
        end
    end
    image:addTouchEventListener(onClick);
end

-- 模型切换动作
function UIPetRecruit:flyAnimaNode(oldNode, newNode, flyType)
    local posY = oldNode:getPositionY();

    -- 设置透明度
    local function setNodeOpacity(node, value)
        -- 递归调用
        for _, childNode in pairs(node:getChildren()) do
            setNodeOpacity(childNode, value);
            node:setCascadeOpacityEnabled(true);
            node:setOpacity(value);
        end
    end

    -- 设置淡入淡出
    local function nodeFadeEffect(node, fadeType)
        -- 递归调用
        for _, childNode in pairs(node:getChildren()) do
            nodeFadeEffect(childNode, fadeType);
            local fadeEffect;

            if fadeType == "fadeOut" then
                fadeEffect = cc.FadeOut:create(0.2);
            else
                fadeEffect = cc.FadeIn:create(0.2);
            end
            node:runAction(fadeEffect);
        end
    end

    local moveTo;
    local moveTo2;
    if flyType == "left" then
        -- 旧节点往左飞出，新节点从右飞入
        moveTo = cc.MoveTo:create(0.2, cc.p(-DESIGN_WIDTH / 2, posY));
        moveTo2 = cc.MoveTo:create(0.2, cc.p(0, posY));
        newNode:setPositionX(DESIGN_WIDTH / 2);
    else
        -- 旧节点往右飞出，新节点从左飞入
        moveTo = cc.MoveTo:create(0.2, cc.p(DESIGN_WIDTH / 2, posY));
        moveTo2 = cc.MoveTo:create(0.2, cc.p(0, posY));
        newNode:setPositionX(-DESIGN_WIDTH / 2);
    end
    oldNode:runAction(moveTo);
    newNode:runAction(moveTo2);
    setNodeOpacity(newNode, 0);
    nodeFadeEffect(oldNode, "fadeOut");
    nodeFadeEffect(newNode, "fadeIn");
end

-- 当商品格子被点击
function UIPetRecruit:onGoodsClick(iconImg)
    if self.iconImg then
        self:checkTipShow(self.iconImg);
    end

    iconImg.focus:setVisible(true);
    iconImg.hint:setVisible(false);

    -- 按钮的提示
    local btnTip = findChildByName(self.node, "BT/btn_desc/hint");
    if ItemM.getAmount(ME.user, iconImg.payAttrib) >= iconImg.goodsPrice then
        btnTip:setVisible(true);
        playBubbleAnima(btnTip, false);
    else
        btnTip:setVisible(false);
    end

    -- 重绘上方信息
    local petNode = findChildByName(self.node, "CT/pet_node");
    local costNode = findChildByName(self.node, "CT/cost_node");
    local costIcon = findChildByName(costNode, "icon");
    local costLabel = findChildByName(costNode, "cost_label");

    local petId = iconImg.classId;
    local modelId = PetM.query(petId, "default_model");
    local animaNode = HeroDisplay.create(modelId);

    self.petId = petId;
    self.campId = CampM.getPetCamp(petId);
    self:redrawCampInfo();

    if self.iconImg and self.animaNode then
        -- 切换模型的
        if iconImg.index > self.iconImg.index then
            -- 点击了右边的格子
            self:flyAnimaNode(self.animaNode, animaNode, "left");
        elseif iconImg.index < self.iconImg.index then
            -- 点击了左边的格子
            self:flyAnimaNode(self.animaNode, animaNode, "right");
        end
    end

    petNode:removeAllChildren();
    petNode:addChild(animaNode);

    self.iconImg = iconImg;
    self.animaNode = animaNode;
    self.nowAction = 1;

    local payItem    = iconImg.payAttrib;   -- 支付材料
    local goodsPrice = iconImg.goodsPrice;  -- 商品价格

    local iconPath = getItemIconPath(ItemM.query(payItem, "icon"));
    local name = ItemM.query(payItem, "name");
    local hasAmount = ItemM.getAmount(ME.user, payItem);
    costIcon:loadTexture(iconPath);
    costIcon:setScale(0.7);
    TextStyleM.setOutlineStyle(costLabel, 3);
    TextStyleM.setTextStyle(costLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_ORANGE, true, -4, nil, nil, nil, nil, true);
    costLabel:setString(string.format(getLocStr("pet_recruit_pay"), name, hasAmount, goodsPrice));

    -- 居中显示
    local gap = 15;
    local labelWidth = getRealContentSize(costLabel).width * 0.85;
    local iconWidth = getRealContentSize(costIcon).width * 0.7;
    local totalWidth = labelWidth + iconWidth + gap;
    costIcon:setPositionX(- totalWidth / 2 + gap * 2);
    costLabel:setPositionX((iconWidth + gap - labelWidth) / 2);
end

-- 绘制阵营
function UIPetRecruit:redrawCampInfo()
    local titleNode = findChildByName(self.node, "TOP/title_node");
    local titleLabel = findChildByName(titleNode, "title_name");

    -- 主标题
    local name;
    if not self.petId and not self.campId then
        name = getLocStr("pet_recruit_title");
    elseif not self.petId then
        name = CampM.query(self.campId, "name");
    else
        name = PetM.query(self.petId, "name");
    end
    TextStyleM.setArtWordTitleStyle(titleLabel);
    titleLabel:setString(name);

    if not self.oldCampId or self.oldCampId ~= self.campId then
        self.oldCampId = self.campId;

        -- 背景
        local bg = findChildByName(self.node, "bg/hero_bg");
        local campIconPath = getHeroBgIconPath(self.campId or 1);
        bg:loadTexture(campIconPath);

        -- 效果
        local effectImg = findChildByName(self.node, "bg/effect_img");
        effectImg:loadTexture(getMercenaryCampIconPath("effect" .. (self.campId or 1)));
        effectImg:stopAllActions();
        effectImg:removeAllChildren();
        local effectList = {1287, 1286, 1288, 1289};
        local _, effect = playEffect(effectImg, effectList[self.campId or 1], 300, 250, nil, 2.35);
        effect:setOpacity(255);
    end
end

-- 显示/隐藏底部BUTTON
function UIPetRecruit:showBottomBotton(visible)
    local BT = findChildByName(self.node, "BT");
    BT:setVisible(visible);
end

-- 检查碎片是否足够提示
function UIPetRecruit:checkTipShow(imgNode)
    imgNode.focus:setVisible(false);

    -- 如果碎片足够则提示
    if ItemM.getAmount(ME.user, imgNode.payAttrib) >= imgNode.goodsPrice then
        imgNode.hint:setVisible(true);
        playBubbleAnima(imgNode.hint, false);
    else
        imgNode.hint:setVisible(false);
    end
end

-- 注册事件处理回调函数
function UIPetRecruit:registerEventCallback()
    -- 注册获得焦点的回调
    EventMgr.register("UIPetRecruit", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UIPetRecruit" then
            UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_MONEY, TOPMENU_FRAME_GEM);
            self:showBottomBotton(true);
            local CT = findChildByName(self.node, "CT");
            CT:setVisible(true);

            if self.needRedraw then
                -- 刷新
                if self.iconImg then
                    self:onGoodsClick(self.iconImg);
                end
                -- 刷新其他的提示
                if self.iconImgList and #self.iconImgList > 0 then
                    for _, node in pairs(self.iconImgList) do
                        if node ~= self.iconImg then
                            self:checkTipShow(node);
                        end
                    end
                end

                self.lastPet = nil;
            end
        elseif para["lose"] == "UIPetRecruit" then
            self:showBottomBotton(false);
            local CT = findChildByName(self.node, "CT");
            CT:setVisible(false);
        end
    end);

    -- 关注激活宠物事件
    EventMgr.register("UIPetRecruit", event.GAIN_PET, function(petId)
        if self.petList then
            table.removeItem(self.petList, petId);
            if #self.petList <= 0 then
                -- 直接关掉自身
                UIMgr.getCurrentScene():removeFormByName("UIPetRecruit");
                return;
            end
        end

        if self.petList and #self.petList == 1 then
            self.scrollview:setVisible(false);
        end

        -- 重绘
        self.iconImg = nil;
        self:redraw();
    end);

    -- 使用罐子事件
    EventMgr.register("UIPetRecruit", event.USE_PUZZLE_BOX, function(para)
        -- 需要检查
        self.needRedraw = true;
        if self.iconImg then
            self.lastPet = self.iconImg.classId;
        end
    end);

    -- 领取奖励事件
    EventMgr.register("UIPetRecruit", event.TAKE_GOLDEN_POT_BONUS, function(args)
        -- 需要检查
        self.needRedraw = true;
        if self.iconImg then
            self.lastPet = self.iconImg.classId;
        end
    end);

    -- 活动开始事件
    EventMgr.register("UIPetRecruit", event.ACTIVITY_CHANGE, function(args)
        local id = args["id"];
        if ActivityM.query(id, "rule") ~= "dungeon" then
            -- 不是地面迷宫
            return;
        end

        if self.petList then
            -- 直接关掉自身
            UIMgr.getCurrentScene():removeFormByName("UIPetRecruit");
        else
            -- 重绘
            self.iconImg = nil;
            self:redraw();
        end
    end);

    -- 活动结束事件
    EventMgr.register("UIPetRecruit", event.ACTIVITY_CLOSE, function(args)
        local id = args["id"];
        if ActivityM.query(id, "rule") ~= "dungeon" then
            -- 不是地面迷宫
            return;
        end

        if self.petList then
            -- 直接关掉自身
            UIMgr.getCurrentScene():removeFormByName("UIPetRecruit");
        else
            -- 重绘
            self.iconImg = nil;
            self:redraw();
        end
    end);

    local hideUI = {"UIMercenaryCamp", } --"UIPetRecruitMain"};
    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIPetRecruit 界面析构清理");
            EventMgr.removeAll("UIPetRecruit");
            for _, name in pairs(hideUI) do
                local node = UIMgr.getCurrentScene():getFormByName(name);
                if node then
                    node:setVisible(true);
                end
            end
        elseif eventType == "enter" then
            for _, name in pairs(hideUI) do
                local node = UIMgr.getCurrentScene():getFormByName(name);
                if node then
                    node:setVisible(false);
                end
            end
        end
    end);
end

-- 注册点击事件
function UIPetRecruit:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIPetRecruit");

            -- 是否跳转至招募主界面
            local function isGotoMainForm()
                -- 如果如下界面是打开的，则不跳转到主界面
                local formList = { "UISkyCityTeam", "UIActivityDungeon", "UIActivityExchange",};
                for _, name in pairs(formList) do
                    if UIMgr.getCurrentScene():getFormByName(name) then
                        return false;
                    end
                end
                return true;
            end

            if isGotoMainForm() then
                -- 跳转到招募主界面
                UIMgr.getCurrentScene():removeFormByName("UIPetRecruitMain");
                local uiPetRecruitMain = UIPetRecruitMain.create();
                UIMgr.getCurrentScene():addForm(uiPetRecruitMain);
            end
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 说明按钮
    local btnDesc = findChildByName(self.node, "BT/btn_desc");
    local function onDescClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.iconImg then
                local iconImg = self.iconImg;
                require "game/ui/form/hero/UIHeroDetailInfo";
                UIMgr.getCurrentScene():removeFormByName("UIHeroDetailInfo");
                uiGoodsDetail = UIHeroDetailInfo.create(iconImg.classId, "petShop", iconImg.goodsId);
                UIMgr.getCurrentScene():addForm(uiGoodsDetail);
            else
                local extra = {
                    ["name"]   = getLocStr("pet_recruit_title"),
                    ["desc"]   = getLocStr("pet_recruit_desc1") .. getLocStr("pet_recruit_desc2") .. getLocStr("pet_recruit_desc3"),
                    ["detail"] = getLocStr("pet_recruit_detail"),
                };
                local uiTankDesc = UITankDesc.create(nil, nil, extra);
                UIMgr.getCurrentScene():addForm(uiTankDesc);

                -- 音效
                AudioM.playFx("workshop_material");
            end
        end
    end
    btnDesc:addTouchEventListener(onDescClick);
    TextStyleM.setTextStyle(btnDesc, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnDesc:setTitleText(getLocStr("btn_text_recruit2"));

    -- 获得碎片按钮
    local btnPot = findChildByName(self.node, "BT/btn_pot");
    local function onPotClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 跳转到黄金罐使用界面
            require "game/ui/form/shop/UITankUse";
            local uiTankUse = UITankUse.create("gem");
            uiTankUse.tankType = "gem";
            uiTankUse.campId = self.campId;
            -- 重绘界面
            uiTankUse.showEffect = true;
            uiTankUse:redraw();
            UIMgr.getCurrentScene():addForm(uiTankUse);
        end
    end
    btnPot:addTouchEventListener(onPotClick);
    TextStyleM.setTextStyle(btnPot, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnPot:setTitleText(getLocStr("btn_text_use_pot"));

    -- 如果活动副本界面是打开的，需要隐藏“获得碎片”按钮
    if self.petList then
        btnPot:setVisible(false);
    end

    local clickPanel = findChildByName(self.node, "CT/click_panel");
    -- 注册节点的点击事件
    clickPanel:setTouchEnabled(true);
    clickPanel:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.animaNode then
                -- 播放受创动画
                if not self.nowAction then
                    self.nowAction = 1;
                end
                self.animaNode:switchAction(heroActions[self.nowAction], true);
                -- 计算下一个动作
                self.nowAction = (self.nowAction + 1) % #heroActions;
                if self.nowAction == 0 then
                    self.nowAction = #heroActions;
                end
            end
        end
    end);
end